package com.haois.mp.wechat.server;

import com.haois.mp.wechat.common.BusinessRemind;
import com.haois.mp.database.module.common.daomain.Parameter;
import com.haois.mp.database.module.wxserver.service.EventHandlerService;
import com.haois.mp.wechat.constants.Enum.MenuEnum;
import com.haois.mp.wechat.util.WxUtil;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * description
 *
 * @author RuiHaoZ on Hgits.cn
 * @date 2018/8/5 15:34
 */
@Component("MoreInfoServer")
public class MoreInfoServerImpl extends AbstractMoreInfoServer {

    @Autowired
    private EventHandlerService eventHandlerService;
    @Autowired
    private MoreInfoController moreInfoController;
    @Value("${defined.wxmp.base-url}")
    private String baseUrl;

    @Value("${defined.wxmp.sys-openid}")
    private String sysOpenId;

    @Override
    public String execute(WxMpXmlMessage wxMpXmlMessage) {
        String content = "";
        String xh = wxMpXmlMessage.getContent();
        Parameter parameter = eventHandlerService.getParameter(xh, MenuEnum.BTN_INFO_MORE.getKey());
        if (parameter != null) {
            if (Parameter.TEXT.equals(parameter.getEvent())) {
                content = parameter.getReply();
                if ("2222".equals(xh)) {
                    content += "\n";
                    String url = WxUtil.transferWxUrl(MenuEnum.BTN_DRIVER_EDIT.getUrl(), MenuEnum.BTN_DRIVER_EDIT.getKey(), baseUrl);
                    content += "    <a href=\"" + url + "\">增加驾驶证</a>";
                    content += "\n";
                    url = WxUtil.transferWxUrl(MenuEnum.BTN_VEHICLE_EDIT.getUrl(), MenuEnum.BTN_VEHICLE_EDIT.getKey(), baseUrl);
                    content += "    <a href=\"" + url + "\">增加行驶证</a>";
                }
            } else if (Parameter.METHOD.equals(parameter.getEvent())) {
                content = moreInfoController.transferChoice(wxMpXmlMessage, parameter);
            } else if (Parameter.AI.equals(parameter.getEvent())) {
                eventHandlerService.freshenEvent(wxMpXmlMessage.getFromUser(), parameter.getEvent(), "TEXT");
                content = parameter.getReply();
            } else {
                content = "更多服务配置有误，请联系管理员";
                BusinessRemind.remindErrorByWx(wxMpXmlMessage.getFromUser(), "更多服务配置有误，请联系管理员", sysOpenId, this.getClass().getName());
            }
        } else {
            content = "序号不正确，退出更多服务模式请发送:Q";
        }
        return content;
    }

}
